package com.xixiake.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

/*import com.alibaba.wukong.auth.AuthService;
import com.alibaba.wukong.im.Conversation;
import com.alibaba.wukong.im.IMEngine;*/
import com.orhanobut.logger.Logger;
import com.xixiake.R;
/*import com.xxk.activity.login.OwnLoginActivity;
import com.xxk.common.AppSetting;
import com.xxk.common.Constants;
import com.xxk.storage.ActivityCollector;
import com.xxk.storage.ActivityManager;
import com.xxk.widget.LoadingDialog;
import com.xxk.widget.listview.PullToRefreshListView;*/

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 意图 工具类（用于页面跳转，显式意图和隐式意图均有重载方法)
 * <p/>
 * Created by Sky on 2015/09/12
 */
public class IntentUtil {

    private static Intent intent;

    /*--------------------------------------------------优化完毕—Start---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 拨打电话逻辑
     *
     * @param activity
     * @param phoneNo
     */
    public static void startPhone(Activity activity, String phoneNo) {
        Intent intent = new Intent();  // 意图是由动作加数据组成
        intent.setAction(Intent.ACTION_CALL); // 可以先设置动作
        intent.setData(Uri.parse("tel:" + phoneNo)); // 设置数据
        activity.startActivity(intent);  // 然后激活意图，调用这个意图
    }

    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 开启一个有下一步动画的activity
     *
     * @param context
     * @param cls
     */
    public static void startActivity(Activity context, Class<?> cls) {
        //Intent intent = new Intent(context, cls);
        context.startActivity(new Intent(context, cls)); // 简写
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);  // 注意:这个方法必须放在startActivity的下面
    }

    /**
     * 开启一个带有返回动画activity
     *
     * @param context
     * @param cls
     */
    public static void startActivityForBackAnimation(Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /**
     * 开启一个有下一步动画的activity，可以设置延迟时间
     *
     * @param context
     * @param cls
     * @param delaytime 延迟执行的时间毫秒
     */
    public static void startActivityForDelay(final Activity context, final Class<?> cls, final long delaytime) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(delaytime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                context.startActivity(new Intent(context, cls));
                context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
            }
        }.start();
    }

    /**
     * 开启一个有下一步动画的activity，可以设置延迟时间，并关闭界面
     *
     * @param context
     * @param cls
     * @param delaytime 延迟执行的时间毫秒
     */
    public static void startActivityForDelayAndFinish(final Activity context, final Class<?> cls, final long delaytime) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(delaytime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                context.startActivity(new Intent(context, cls));
                context.finish();
                context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
            }
        }.start();
    }


    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 关闭activity（前进动画）
     */
    /*public static void finishAct() {
        ActivityManager.getInstance().getCurrentActivity().finish();
        ActivityManager.getInstance().getCurrentActivity().overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/

    /**
     * 关闭activity（前进动画，收键盘）
     */
    /*public static void finishActivityAndColseKeyboard() {
        ActivityManager.getInstance().getCurrentActivity().finish();
        ActivityManager.getInstance().getCurrentActivity().overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
        KeyBoardUtil.dismissSoftKeyboard();
    }*/

    /**
     * 关闭activity（返回动画）
     */
    /*public static void finishActForBackAni() {
        ActivityManager.getInstance().getCurrentActivity().finish();
        ActivityManager.getInstance().getCurrentActivity().overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }*/

    /**
     * 关闭activity（返回动画，收键盘）
     */
    /*public static void finishActForBackAniAndCloseKeyboard() {
        ActivityManager.getInstance().getCurrentActivity().finish();
        ActivityManager.getInstance().getCurrentActivity().overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
        KeyBoardUtil.dismissSoftKeyboard();
    }*/

    /**
     * 关闭全部activity（带返回动画）
     */
    /*public static void finishActivityCollectllorAllActivityForBackAnimation() {
        ActivityCollector.finishAll();
        ActivityManager.getInstance().getCurrentActivity().overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
        KeyBoardUtil.dismissSoftKeyboard();
    }*/

    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /*--------------------------------------------------优化完毕—End---------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 开启一个有下一步动画的activity，并关闭界面
     *
     * @param context
     * @param cls
     */
    public static void startActivityAndFinish(Activity context, Class<?> cls) {
        context.startActivity(new Intent(context, cls));
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity，并关闭界面，带返回动画
     *
     * @param context
     * @param cls
     */
    public static void startActivityAndFinishForBackAnimation(Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /**
     * 开启一个activity，并关闭被全局应用（Application）保存的所有界面（前进动画）
     *
     * @param context
     * @param cls
     * @param activityStack
     */
    public static void startActivityAndFinishAll(Activity context, Class<?> cls, List<Activity> activityStack) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        for (Activity act : activityStack) {
            act.finish();
        }
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity，并关闭被全局应用（Application）保存的所有界面（返回动画）
     *
     * @param context
     * @param cls
     * @param activityStack
     */
    public static void startActivityAndFinishAllForBackAnimation(Activity context, Class<?> cls, List<Activity> activityStack) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        for (Activity act : activityStack) {
            act.finish();
        }
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }



    /**
     * 开启一个activity并携带map类型数据，支持多种类型（需要可以自己继续加判断类型）
     * Created by henry
     *
     * @param context
     * @param cls
     * @param map
     */
    public static void startActivityForMap(Activity context, Class<?> cls, Map<String, Object> map) {
        Intent intent = new Intent(context, cls);
        for (String key : map.keySet()) {
            Object value = map.get(key);
            if (value instanceof String) {
                intent.putExtra(key, (String) map.get(key));
            } else if (value instanceof Integer) {
                intent.putExtra(key, (Integer) map.get(key));
            } else if (value instanceof Boolean) {
                intent.putExtra(key, (Boolean) map.get(key));
            } else if (value instanceof Serializable) {
                intent.putExtra(key, (Serializable) map.get(key));
            }
        }
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    public static void startActivityForMapForBackAnimation(Activity context, Class<?> cls, Map<String, Object> map) {
        Intent intent = new Intent(context, cls);
        for (String key : map.keySet()) {
            Object value = map.get(key);
            if (value instanceof String) {
                intent.putExtra(key, (String) map.get(key));
            } else if (value instanceof Integer) {
                intent.putExtra(key, (Integer) map.get(key));
            } else if (value instanceof Boolean) {
                intent.putExtra(key, (Boolean) map.get(key));
            } else if (value instanceof Serializable) {
                intent.putExtra(key, (Serializable) map.get(key));
            }
        }
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /**
     * 开启一个activity并携带字符串类型数据(1组)
     *
     * @param activity
     * @param cls
     * @param key
     * @param value
     */
    public static void startActivityForPutString(Activity activity, Class<?> cls, String key, String value) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra(key, value);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity并携带字符串类型数据(1组)
     *
     * @param activity
     * @param cls
     * @param key
     * @param value
     */
    public static void startActivityForPutStringAndFinishForBackAnimation(Activity activity, Class<?> cls, String key, String value) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra(key, value);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /**
     * 开启一个activity并携带字符串类型数据(3组)
     *
     * @param context
     * @param cls
     * @param key1
     * @param value1
     * @param key2
     * @param value2
     * @param key3
     * @param value3
     */
    public static void startActivityForPutString(Activity context, Class<?> cls, String key1, String value1, String key2, String value2, String key3, String value3) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key1, value1);
        intent.putExtra(key2, value2);
        intent.putExtra(key3, value3);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity并携带字符串类型数据(2组)
     *
     * @param context
     * @param cls
     * @param key1
     * @param value1
     * @param key2
     * @param value2
     */
    public static void startActivityForPutString(Activity context, Class<?> cls, String key1, String value1, String key2, String value2) {
        Intent intent = context.getIntent();
        intent = new Intent(context, cls);
        intent.putExtra(key1, value1);
        intent.putExtra(key2, value2);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity并携带int类型数据(1组)
     *
     * @param context
     * @param cls
     * @param key
     * @param value
     */
    public static void startActivityForPutInt(Activity context, Class<?> cls, String key, int value) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key, value);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个activity（跳转到对应的Tab）
     *
     * @param context
     * @param cls
     */
    /*public static void startActivityForTab(int tab, Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.TABNUMBER, tab); // TAB_OWN:4，关键逻辑
        context.startActivity(intent);
        context.setResult(0, intent);
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/

    /**
     * 开启一个activity（跳转到指定的Tab页面）
     *
     * @param context
     * @param cls
     */
    /*public static void startActivityForTab(int tab, Activity context, Class<?> cls, String content) {
        intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //intent.putExtra(Constants.TABNUMBER, tab); //0123分别对应4个Tab
        SharedPreferencesUtil.putInt(Constants.TABNUMBER, tab);
        SharedPreferencesUtil.putString(Constants.RETURN_TYPE, content);
        SharedPreferencesUtil.putString("CustomTabhost_Flag", "IntentUtil");
        context.startActivity(intent);
        context.setResult(0, intent);
        //context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/


    /*public static void startActivityForTab(Activity context, Class<?> cls, int tab) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.TABNUMBER, tab);
        context.startActivity(intent);
        context.setResult(0, intent);
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/

    //增加Conversation参数
    /*public static void startActivityForTabWithConversation(Activity context, Class<?> cls, int tab, Conversation conversation) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.TABNUMBER, tab);
        Bundle bundle = new Bundle();
        bundle.putSerializable("converation", conversation);
        intent.putExtras(bundle);
        context.startActivity(intent);
        context.setResult(0, intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/

    //跳转到官方通知列表
  /*  public static void startActivityForTabWithNotificationTag(Activity context, Class<?> cls, int tab, int a) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.TABNUMBER, tab);
        intent.putExtra(Constants.OFFICAL_TAG, a);
        context.startActivity(intent);
        context.setResult(0, intent);
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/

    /**
     * 回退一个activity（跳转到指定的Tab页面）
     *
     * @param context
     * @param cls
     */
    /*public static void startActivityForBackTab(int a, Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.TABNUMBER, a); //0123分别对应4个Tab
        context.startActivity(intent);
        context.setResult(0, intent);
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }*/

    /**
     * 直接跳转开启进入登录页面
     *
     * @param activity
     */
    /*public static void startOwnLoginActivity(Activity activity) {
        ToastUtil.show(activity, activity.getResources().getString(R.string.toast_login));
        //Intent intent = new Intent(activity, OwnLoginActivity.class);
        activity.startActivity(new Intent(activity, OwnLoginActivity.class));
        activity.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }*/


    /**
     * 开启一个设置结果码并关闭界面的Activity
     *
     * @param context
     * @param cls
     */
    public static void startActivitySetResultAndFinish(Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.setResult(0, intent);// 把结果数据设置前面的界面, 结果码不知道写0,设置结果的意图对象
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
    }

    /**
     * 开启一个设置结果码并关闭界面的Activity
     *
     * @param context
     * @param cls
     */
    public static void startActivitySetResultAndFinishForBackAnimation(Activity context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.setResult(0, intent);// 把结果数据设置前面的界面, 结果码不知道写0,设置结果的意图对象
        context.finish();
        context.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /*public static void startActivityForResultToKicked(PullToRefreshListView pullToRefreshListView, LoadingDialog loadingDialog, String TAG, int errorCode, String errorMsg, Activity activity, int callbackFlag, String putStringContent) {
        if (pullToRefreshListView == null) {
            ToastUtil.showForFlag(activity, "pullToRefreshListView为空");
        } else if (pullToRefreshListView != null) {
            pullToRefreshListView.onRefreshComplete();
        }

        if (loadingDialog == null) {
            ToastUtil.showForFlag(activity, "loadingDialog为空");
        } else if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }

        if (AppSetting.isLoggerDebugVersion) {
            Logger.t(TAG).e("errorCode: %d,errorMsg: %s", errorCode, errorMsg);
        }

        ToastUtil.show(activity, activity.getResources().getString(R.string.toast_login_state_10010));

        intent = new Intent(activity, OwnLoginActivity.class);
        SharedPreferencesUtil.putString(Constants.RETURN_TYPE, putStringContent);
        activity.startActivityForResult(intent, callbackFlag);
        activity.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);

        IMEngine.getIMService(AuthService.class).logout();
    }*/

    /*public static void startActivityForResultToKickedForDelay(PullToRefreshListView pullToRefreshListView, LoadingDialog loadingDialog, String TAG, int errorCode, String errorMsg, final Activity activity, final int callbackFlag, String putStringContent) {
        if (pullToRefreshListView == null) {
            ToastUtil.showForFlag(activity, "pullToRefreshListView为空");
        } else if (pullToRefreshListView != null) {
            pullToRefreshListView.onRefreshComplete();
        }
        if (loadingDialog == null) {
            ToastUtil.showForFlag(activity, "loadingDialog为空");
        } else if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        if (AppSetting.isLoggerDebugVersion) {
            Logger.t(TAG).e("errorCode: %d,errorMsg: %s", errorCode, errorMsg);
        }
        ToastUtil.show(activity, activity.getResources().getString(R.string.toast_login_state_10010));
        intent = new Intent(activity, OwnLoginActivity.class);
        SharedPreferencesUtil.putString(putStringContent, putStringContent);
        UIUtil.postDelayed(new Runnable() {
            @Override
            public void run() {
                activity.startActivityForResult(intent, callbackFlag);
                activity.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
            }
        }, 500);
        IMEngine.getIMService(AuthService.class).logout();
    }*/



    /**
     * 关闭activity，带前进动画，并收键盘
     */
   /* public static void finishActivity(Activity activity) {
        activity.finish();
        activity.overridePendingTransition(R.anim.trans_pre_in, R.anim.trans_pre_out);
        KeyBoardUtil.dismissSoftKeyboard(activity);
    }*/

    /**
     * 关闭activity，带返回动画并收键盘
     */
    /*public static void finishActivityForBackAnimation(Activity activity) {
        activity.finish();
        activity.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
        KeyBoardUtil.dismissSoftKeyboard(activity);
    }*/

    /**
     * 关闭activity，带返回动画
     */
    /*public static void finishActivityCollectllorAllActivityForBackAnimation(Activity activity) {
        ActivityCollector.finishAll();
        activity.overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
        KeyBoardUtil.dismissSoftKeyboard(activity);
    }*/

    /**
     * @param packageContext
     * @param cls
     * @param keyvalues      需要传进去的String参数{{key1,values},{key2,value2}...}
     * @Description: 跳转, 带参数的方法;需要其它的数据类型,再继续重载吧
     */
    public static void toNextActivity(Context packageContext, Class<?> cls, String[][] keyvalues) {
        Intent i = new Intent(packageContext, cls);
        for (String[] strings : keyvalues) {
            i.putExtra(strings[0], strings[1]);
        }
        packageContext.startActivity(i);
    }

    /**
     * @param packageContext
     * @param action         含操作的Intent
     * @Description: 隐式启动, 跳转
     */
    public static void startActivityIntentSafe(Context packageContext, Intent action) {
        PackageManager packageManager = packageContext.getPackageManager();
        List activities = packageManager.queryIntentActivities(action, PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafe = activities.size() > 0;
        if (isIntentSafe) {
            packageContext.startActivity(action);
        }
    }

    public static void toNextActivityAndFinish(Context packageContext, Class<?> cls) {
        Intent i = new Intent(packageContext, cls);
        packageContext.startActivity(i);
        ((Activity) packageContext).finish();
    }

    public static void finishWithAni(Context context) {
        ((Activity) context).finish();
        ((Activity) context).overridePendingTransition(R.anim.trans_pre_in_back, R.anim.trans_pre_out_back);
    }

    /*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
}
